package com.hmy.uaa.controller.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmy.uaa.bo.request.ReqPage;
import com.hmy.uaa.bo.request.RoleMenuRequest;
import com.hmy.uaa.bo.request.RoleRequest;
import com.hmy.uaa.po.Role;
import com.hmy.uaa.service.RoleMenuRelationService;
import com.hmy.uaa.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.groups.Default;


@Api(tags = "角色接口")
@Validated
@RestController
@RequestMapping("/api/role")
@Slf4j
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleMenuRelationService roleMenuRelationService;

    @PostMapping
    @ApiOperation("新增角色")
    public void add(@RequestBody @Validated({RoleRequest.Insert.class, Default.class}) RoleRequest roleRequest) {
        log.info(roleRequest.toString());
        roleService.save(roleRequest);
    }

    @PutMapping
    @ApiOperation("修改角色")
    public void update(@RequestBody @Validated({RoleRequest.Update.class, Default.class}) RoleRequest roleRequest) {
        log.info(roleRequest.toString());
        roleService.update(roleRequest);
    }

    @ApiOperation("删除角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键id")
    })
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        log.info("id:{}", id);
        roleService.delete(id);

    }

    @ApiOperation("查询角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "角色名称")
    })
    @GetMapping("/page")
    public Page<Role> queryPage(ReqPage reqPage,
                                @RequestParam(value = "name", required = false) String name) {

        Page<Role> page = new Page<>(reqPage.getPageNumber(), reqPage.getPageSize());
        return roleService.queryPage(page, name);
    }

    @ApiOperation("菜单权限")
    @PostMapping("/menu")
    public void bindMenu(@RequestBody @Validated({RoleMenuRequest.Insert.class, Default.class}) RoleMenuRequest roleMenuRequest) {
        log.info(roleMenuRequest.toString());
        roleMenuRelationService.bindMenu(roleMenuRequest);
    }
}
